/**
 * 
 */
package com.gopay.bis.psbc.ebank.operation;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.psbc.ebank.PsbcB2bMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;
import com.psbc.payment.client.SignatureService;

/**
 * @PsbcB2bCertHelper.java
 * @author Ryan
 * @2013-12-2 上午10:34:24  www.gopay.com.cn Inc.All rights reserved.
 */
public class PsbcB2bCertHelper implements ICertHelper {
    
    public String sign(BaseMessage baseMessage) throws GopayException {
        try {
            PsbcB2bMessage psbcB2bMessage  = (PsbcB2bMessage)baseMessage;
            String plainStr = psbcB2bMessage.getPlainMessage();
            BankLogUtils.printB2BSignPlainLog(psbcB2bMessage);
            String sinMsg = SignatureService.sign(plainStr);
            BankLogUtils.printB2BSignSignatureLog(psbcB2bMessage, sinMsg);
            return sinMsg;
        } catch (Exception e) {
            BankLogUtils.printB2BSignExceptionLog(baseMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6028);
        }
    }

    public Object verify(BaseMessage baseMessage) throws GopayException {
        try {
            PsbcB2bMessage psbcB2bMessage  = (PsbcB2bMessage)baseMessage;
            BankLogUtils.printB2BVerifyPlainLog(psbcB2bMessage);
            boolean isVeriy = SignatureService.verify(psbcB2bMessage.getPlainMessage(), psbcB2bMessage.getSignMessage());
            BankLogUtils.printB2BVerifyResultLog(psbcB2bMessage, true);
            return isVeriy;
        } catch (Exception e) {
            BankLogUtils.printB2BVerifyExceptionLog(baseMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6030);
        }
    }

}
